|
pow x 0 = 1 pow x n = if even n then pxn2 * pxn2 else x * pow x (n-1) where pxn2 = pow x (n/2) f x = pow x 5 Since n is known we can specialise pow in its second argument and unfold the recursive calls: pow5 x = x * x4 where x4 = x2 * x2 x2 = x * x f x = pow5 x pow5 is known as the residual. We could now also unfold pow5 giving: f x = x * x4 where x4 = x2 * x2 x2 = x * x It is important that the partial evaluation algorithm should terminate. This is not guaranteed in the presence of recursive function definitions. For example, if partial evaluation were applied to the right hand side of the second clause for pow above, it would never terminate because the valu スポンサード リンク
|